﻿using MediatR;
using SlaughterItem.Common.Domains.Manages.Addr;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace GetSlaughterItem.API.Application.Commands.Week01.Addrs
{
    /// <summary>
    /// 区
    /// </summary>
    public class AreasSuretenHandler : IRequestHandler<AreasSuretenComend, Result>
    {
        private readonly IBaseRepositoryGet<Citys> Citys;
        private readonly IBaseRepositoryGet<Areas> Areas;

        public AreasSuretenHandler(IBaseRepositoryGet<Areas> Areas, IBaseRepositoryGet<Citys> Citys)
        {
            this.Areas = Areas;
            this.Citys = Citys;
        }


        public async Task<Result> Handle(AreasSuretenComend request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            try
            {
                var list = (from a in Citys.GetAll()
                           join b in Areas.GetAll() on a.CityId equals b.CityId
                           where a.CityId == request.CityId
                           select new
                           {
                               a.CityId,
                               a.CitysName,
                               b.AreaId,
                               b.AreaName,
                           }).ToList();


                if (list != null)
                {
                    result.Code = (int)CommonErrorCode.Success;
                    result.Data = list;
                    result.Msg = "查询成功";
                }
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
